﻿using System;
using System.Collections;
using Gp.Scripts.Core;

namespace Gp.Scripts.Data {
    /// <summary>
    /// 根据回合开始进行更新。默认持续为1回合，默认回合开始时进行持续时长更新
    /// </summary>
    public abstract class SimpleBuff<T> : BaseBuff<T>, IAdditiveBuff where T : BuffEntityConfig {
        /// <summary>
        /// 持续回合数
        /// </summary>
        public int Duration { get; set; } = 1;

        public bool IsPersistent { get; set; } = false;


        private int _level = 1;

        public virtual int Level {
            get => _level;
            set => _level = Math.Max(value, 0);
        }


        protected bool IsUnitReference(AttackActionRequest request) {
            return request.Source == Unit;
        }

        public override IEnumerator TurnStartUpdate() {
            if (IsPersistent)
                yield break;

            Duration--;
            if (Duration <= 0) {
                Destroy();
            }
        }


        public override BuffIconRenderInfo OnIconRender() {
            var temp = base.OnIconRender();
            if (! IsPersistent)
                temp.Duration = this.Duration;
            if (Level > 0) {
                temp.BuffName += " lv." + Level;
            }
            return temp;
        }


        /// 当叠加时，增加持续回合数
        public override void OnBuffAdditive(BaseBuff other) {
            if (other is SimpleBuff<T> o) this.Duration += o.Duration;
        }

    }
}